package com.synnex.mobile.servier.core.notification.service.impl;

import com.synnex.mobile.generic.core.util.ObjectUtil;
import com.synnex.mobile.servier.core.notification.bo.DeviceTokenBo;
import com.synnex.mobile.servier.core.notification.dao.DeviceTokenDao;
import com.synnex.mobile.servier.core.notification.dto.DeviceToken;
import com.synnex.mobile.servier.core.notification.service.DeviceTokenService;
import com.synnex.mobile.servier.core.service.impl.AbstractServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * To change this template use File | Settings | File Templates.
 */
@Transactional
public class DeviceTokenServiceImpl extends AbstractServiceImpl implements DeviceTokenService {

    protected DeviceTokenDao deviceTokenDao;

    public void setDeviceTokenDao(DeviceTokenDao deviceTokenDao) {
        this.deviceTokenDao = deviceTokenDao;
    }

    public List<DeviceTokenBo> getDeviceToken() {

        List<DeviceToken> tokens = deviceTokenDao.getTokens();

        return DeviceTokenBo.createListInstances(tokens);
    }

    public void addDeviceToken(DeviceTokenBo tokenBo) {

        List<DeviceToken> tokens = deviceTokenDao.findToken(tokenBo.getToken());
        if(null == tokens || tokens.isEmpty()){

            DeviceToken token = ObjectUtil.copyProperties(tokenBo,DeviceToken.class);
            token.setEntryDate(new Date());
            deviceTokenDao.addToken(token);
        }


    }
}
